﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using TLL.Finance.BankSys;
using TLL.Finance.Currencys;

namespace TasksLog
{
    public partial class DetailCard : Form
    {
        private bool MouseClick = false;
        private Loader Loader = null;
        private Point MouseClickPosition;

        string card = string.Empty;
        List<BankMerchant> merchants = new List<BankMerchant>();
        public DetailCard(string card, List<BankMerchant> merchants)
        {
            this.card = card;
            this.merchants = merchants;
            InitializeComponent();
            InitializeData();
            this.StartPosition = FormStartPosition.CenterScreen;
        }
        private void InitializeData()
        {
            if (Loader == null)
                Loader = new Loader(this, LoaderType.Diagram);

            Loader.Start();

            var privateAPI = new PrivateAPI("114", this.merchants.Where(x=>x.Card == this.card).ToList());
            var res = privateAPI.GetAccountStatements();

            foreach (var invoice in res.Take(30))
            {
                LBCardNumber.Items.Add(invoice.CardNumber);
                LBTranDate.Items.Add(invoice.TranDate.ToShortDateString());
                LBAmount.Items.Add(invoice.FormatAmount);
                LBRest.Items.Add(invoice.Rest.ToString());
                LBDetail.Items.Add(invoice.Detail);
            }

            double count = 0;
            foreach (var amount in res.Where(x=>x.InCard).Select(x=>x.Amount))
                count += amount;
            LBAmount.Items.Add("--------------------------------");

            var countFormat = string.Empty;
            switch (this.merchants.FirstOrDefault(x=>x.Card == this.card).CurrencyType)
            {
                case CurrencyType.RUB: countFormat = count.ToString("0,000.0") + " RUB"; break;
                case CurrencyType.UAH: countFormat = count.ToString("0, 000.0") + " UAH"; break;
                case CurrencyType.USD: countFormat = count.ToString("C"); break;
            }

            LBAmount.Items.Add("         +" + countFormat);

            Loader.Stop();
            this.WindowState = FormWindowState.Normal;
        }
        private void LBAmount_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index != -1)
            {
                e.DrawBackground();
                Brush myBrush = Brushes.Yellow;
                if (LBAmount.Items[e.Index].ToString() != string.Empty)
                    switch (LBAmount.Items[e.Index].ToString()[0])
                    {
                        case '+': myBrush = Brushes.Green; break;
                        case '-': myBrush = Brushes.Red; break;
                        default: myBrush = Brushes.RoyalBlue; break;
                    }

                if (e.Index > LBAmount.Items.Count - 2 || e.Index == 30)
                    myBrush = Brushes.DarkGreen;

                e.Graphics.DrawString(LBAmount.Items[e.Index].ToString(),
                    e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
                e.DrawFocusRectangle();
            }
        }
        private void LBRest_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index != -1)
            {
                e.DrawBackground();
                Brush myBrush = Brushes.Yellow;
                if (LBAmount.Items[e.Index].ToString() != string.Empty)
                    switch (LBAmount.Items[e.Index].ToString()[0])
                    {
                        case '+': myBrush = Brushes.Green; break;
                        case '-': myBrush = Brushes.Red; break;
                        default: myBrush = Brushes.RoyalBlue; break;
                    }

                e.Graphics.DrawString(LBRest.Items[e.Index].ToString(),
                    e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
                e.DrawFocusRectangle();
            }
        }

        private void BtClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtInDock_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void LB_SelectedIndexChanged(object sender, EventArgs e)
        {
            var LB = sender as ListBox;

            LBCardNumber.SelectedIndex = LB.SelectedIndex;
            LBAmount.SelectedIndex = LB.SelectedIndex;
            LBDetail.SelectedIndex = LB.SelectedIndex;
            LBRest.SelectedIndex = LB.SelectedIndex;
            LBTranDate.SelectedIndex = LB.SelectedIndex;
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {

            this.MouseClick = true;
            this.MouseClickPosition = new Point(e.Location.X, e.Location.Y);
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            this.MouseClick = false;
            this.MouseClickPosition = Point.Empty;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.MouseClick)
            {
                this.Top = MousePosition.Y - this.MouseClickPosition.Y;
                this.Left = MousePosition.X - this.MouseClickPosition.X;
            }
        }

        private void BtClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtInDock_Click_1(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
}
